import requests

class APIClient:
    def __init__(self,
                 api_key,
                 base_url="https://api.deepseek.com/v1/",
                 api_type="deepseek",
                 max_tokens=8192,
                 temperature=0.2):
        self.api_key = api_key
        self.base_url = base_url
        self.headers = {"Authorization": f"Bearer {api_key}"}
        self.id = None
        self.api = api_type
        self.max_tokens = max_tokens
        self.temperature = temperature

    def post_request(self, endpoint, data):
        response = requests.post(
            f"{self.base_url}{endpoint}",
            headers=self.headers,
            json=data,
        )

        # print("API Response:", response.text)  # 打印调试信息
        if response.status_code != 200:
            raise Exception(f"API Error: {response.status_code}, {response.text}")
        try:
            return response.json()
        except ValueError as e:
            raise Exception(f"Failed to decode JSON: {e}, Response: {response.text}")

    def upload_file(self, endpoint, file_path):
        files = {"file": open(file_path, "rb")}
        data = {'purpose': 'file-extract'}
        response = requests.post(
            f"{self.base_url}{endpoint}",
            headers=self.headers,
            files=files,
            data=data
        )
        if response.status_code == 200:
            self.id = response.json()['id']
            return response.json()['id']
        else:
            raise Exception(f"API Error: {response.status_code}, {response.text}")

